Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR modifies commandline_utility.py to avoid shell injection risks by using shell=False with argv form on non-Windows platforms, and subprocess.list2cmdline with shell=True on Windows. It also adds unit tests for the run_cli_command function.
Changes:
- Replaces
shell=Truewith platform-aware subprocess invocation to mitigate shell injection - Adds a proper unit test file for
run_cli_command - Accidentally embeds test functions and a
unittest.mockimport in production code
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
sdk/ml/azure-ai-ml/azure/ai/ml/_local_endpoints/utilities/commandline_utility.py |
Platform-aware shell handling; unfortunately also adds test code and unittest.mock import to production |
sdk/ml/azure-ai-ml/tests/local_endpoint/unittests/test_commandline_utility.py |
New unit tests covering stderr redirection, shell metacharacters, JSON parsing, and error propagation |
| import subprocess | ||
| import sys | ||
| import time | ||
| from unittest import mock |
Comment on lines
+126
to
+173
| def _test_run_cli_command_stderr_to_stdout_true(): | ||
| """Internal test to validate subprocess arguments when stderr_to_stdout is True.""" | ||
| cmd = ["echo", "hello"] | ||
| custom_env = {"FOO": "BAR"} | ||
| with mock.patch("subprocess.check_output") as check_output_mock: | ||
| check_output_mock.return_value = b"" | ||
| run_cli_command( | ||
| cmd_arguments=cmd, | ||
| custom_environment=custom_env, | ||
| return_json=False, | ||
| timeout=None, | ||
| do_not_print=True, | ||
| stderr_to_stdout=True, | ||
| ) | ||
| # Verify argv (first positional argument) is passed through unchanged. | ||
| assert check_output_mock.call_args is not None | ||
| called_args, called_kwargs = check_output_mock.call_args | ||
| assert called_args[0] == cmd | ||
| # Verify shell and stderr behavior. | ||
| assert called_kwargs.get("shell") is False | ||
| assert called_kwargs.get("stderr") is subprocess.STDOUT | ||
| # Verify environment is forwarded. | ||
| assert called_kwargs.get("env") == custom_env | ||
|
|
||
|
|
||
| def _test_run_cli_command_stderr_to_stdout_false(): | ||
| """Internal test to validate subprocess arguments when stderr_to_stdout is False.""" | ||
| cmd = ["echo", "hello"] | ||
| custom_env = {"FOO": "BAR"} | ||
| with mock.patch("subprocess.check_output") as check_output_mock: | ||
| check_output_mock.return_value = b"" | ||
| run_cli_command( | ||
| cmd_arguments=cmd, | ||
| custom_environment=custom_env, | ||
| return_json=False, | ||
| timeout=None, | ||
| do_not_print=True, | ||
| stderr_to_stdout=False, | ||
| ) | ||
| # Verify argv (first positional argument) is passed through unchanged. | ||
| assert check_output_mock.call_args is not None | ||
| called_args, called_kwargs = check_output_mock.call_args | ||
| assert called_args[0] == cmd | ||
| # Verify shell behavior and absence of stderr redirection. | ||
| assert called_kwargs.get("shell") is False | ||
| assert "stderr" not in called_kwargs | ||
| # Verify environment is forwarded. | ||
| assert called_kwargs.get("env") == custom_env |
Comment on lines
+124
to
+173
|
|
||
|
|
||
| def _test_run_cli_command_stderr_to_stdout_true(): | ||
| """Internal test to validate subprocess arguments when stderr_to_stdout is True.""" | ||
| cmd = ["echo", "hello"] | ||
| custom_env = {"FOO": "BAR"} | ||
| with mock.patch("subprocess.check_output") as check_output_mock: | ||
| check_output_mock.return_value = b"" | ||
| run_cli_command( | ||
| cmd_arguments=cmd, | ||
| custom_environment=custom_env, | ||
| return_json=False, | ||
| timeout=None, | ||
| do_not_print=True, | ||
| stderr_to_stdout=True, | ||
| ) | ||
| # Verify argv (first positional argument) is passed through unchanged. | ||
| assert check_output_mock.call_args is not None | ||
| called_args, called_kwargs = check_output_mock.call_args | ||
| assert called_args[0] == cmd | ||
| # Verify shell and stderr behavior. | ||
| assert called_kwargs.get("shell") is False | ||
| assert called_kwargs.get("stderr") is subprocess.STDOUT | ||
| # Verify environment is forwarded. | ||
| assert called_kwargs.get("env") == custom_env | ||
|
|
||
|
|
||
| def _test_run_cli_command_stderr_to_stdout_false(): | ||
| """Internal test to validate subprocess arguments when stderr_to_stdout is False.""" | ||
| cmd = ["echo", "hello"] | ||
| custom_env = {"FOO": "BAR"} | ||
| with mock.patch("subprocess.check_output") as check_output_mock: | ||
| check_output_mock.return_value = b"" | ||
| run_cli_command( | ||
| cmd_arguments=cmd, | ||
| custom_environment=custom_env, | ||
| return_json=False, | ||
| timeout=None, | ||
| do_not_print=True, | ||
| stderr_to_stdout=False, | ||
| ) | ||
| # Verify argv (first positional argument) is passed through unchanged. | ||
| assert check_output_mock.call_args is not None | ||
| called_args, called_kwargs = check_output_mock.call_args | ||
| assert called_args[0] == cmd | ||
| # Verify shell behavior and absence of stderr redirection. | ||
| assert called_kwargs.get("shell") is False | ||
| assert "stderr" not in called_kwargs | ||
| # Verify environment is forwarded. | ||
| assert called_kwargs.get("env") == custom_env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Please add an informative description that covers that changes made by the pull request and link all relevant issues.
If an SDK is being regenerated based on a new API spec, a link to the pull request containing these API spec changes should be included above.
All SDK Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines